package futuremode;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * jdk的future模式
 */
public class JDKFuture {

    private static class Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            Thread.sleep(5000);
            return "call return ";
        }
    }

    public static void main(String[] args) throws Exception {

        List<Future<String>> futures = new ArrayList<Future<String>>();
        ExecutorService executorService = Executors.newCachedThreadPool();

        System.out.println("已经提交资源申请");
        for (int i = 0; i < 10; i++) {
            futures.add(executorService.submit(new Task()));
        }

        for (Future<String> future : futures) {
            // 判断资源是不是已经准备完毕，准备完毕直接获取。
            if (!future.isDone()) {
                System.out.println("资源还没有准备好");
            }
            System.out.println(future.get()); //future.get()是阻塞方法
        }
        executorService.shutdown();
    }


}
